//
//  BCTBShopCarViewController.m
//  BCOpenTb
//
//  Created by yuhanle on 2017/1/16.
//  Copyright © 2017年 tilink. All rights reserved.
//

#import "BCTBShopCarViewController.h"
#import <AlibcTradeSDK/AlibcTradeSDK.h>

@interface BCTBShopCarViewController () <UIWebViewDelegate> {
    BOOL _hasAppear;
}

@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *waiterView;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *backBarBtn;

@end

@implementation BCTBShopCarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    if ([self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)]) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    self.navigationItem.leftBarButtonItem = nil;
    
    id <AlibcTradePage> page = [AlibcTradePageFactory myCartsPage];
    id <AlibcTradeService> service = [AlibcTradeSDK sharedInstance].tradeService;
    AlibcTradeShowParams *showParams = [[AlibcTradeShowParams alloc] init];
    showParams.openType = ALiOpenTypeH5;
    [service show:self webView:self.webView page:page showParams:showParams taoKeParams:nil trackParam:nil tradeProcessSuccessCallback:^(AlibcTradeResult * _Nullable result) {
        
    } tradeProcessFailedCallback:^(NSError * _Nullable error) {
        
    }];
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    
}
    
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [self.waiterView stopAnimating];
    
    if ([self.webView canGoBack]) {
        self.navigationItem.leftBarButtonItem = self.backBarBtn;
    }else {
        self.navigationItem.leftBarButtonItem = nil;
    }
}
    
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    [self.waiterView stopAnimating];
}

- (IBAction)backBarBtnEvent:(UIBarButtonItem *)sender {
    [self.webView goBack];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    if (![self.webView canGoBack] && _hasAppear) {
        [self.webView reload];
    }
    
    _hasAppear = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
